﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Bullet : MonoBehaviour
{
    public GameObject Source;
    [HideInInspector]
    public int energy;
    [HideInInspector]
    public float shotAngle;

    private void Awake()
    {
        StartCoroutine(Hajime());
    }

    private IEnumerator Hajime()
    {
        yield return new WaitForSeconds(0.15f);
        GetComponent<TrailRenderer>().enabled = true;
    }

    private void Start()
    {
        transform.localScale = new Vector3(energy +0.15f , energy + 0.15f, 1) * 0.15f;
        shotAngle = transform.eulerAngles.z;

        Vector2 Direction = new Vector2(Mathf.Cos(transform.eulerAngles.z * Mathf.PI / 180), Mathf.Sin(transform.eulerAngles.z * Mathf.PI / 180));
        GetComponent<Rigidbody2D>().velocity = Direction / (Direction.magnitude) * BaseData.Instance.bulletSpeed;
    }

    private void Update()
    {
    }

    public void Die()
    {
        GameObject clone = Instantiate(Resources.Load(BaseData.Instance.Pref_explode)) as GameObject;
        clone.transform.position = transform.position;
        clone.transform.Find("ex (5)").localScale = new Vector3(energy, energy, 1) * 0.35f;
        Destroy(clone, 0.7f);
        Destroy(gameObject);
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.tag == "wall" || collision.gameObject.tag == "bullet")
        {
            Die();
        }
    }
}